using System;
using OracleInternal.Json.Common;

namespace OracleInternal.Json.Sql
{
	internal abstract class OracleJsonNumber : OracleJsonValue
	{
		internal abstract decimal BigDecimalValue();

		internal bool IsIntegral()
		{
			decimal num = BigDecimalValue();
			if (num == 0m)
			{
				return true;
			}
			return ((decimal.GetBits(num)[3] >> 16) & 0x7F) == 0;
		}

		internal int IntValue()
		{
			return (int)Math.Truncate(BigDecimalValue());
		}

		internal int IntValueExact()
		{
			decimal num = BigDecimalValue();
			decimal num2 = (int)Math.Truncate(num);
			if (num - num2 != 0m)
			{
				throw new ArithmeticException();
			}
			return (int)num2;
		}

		internal long LongValue()
		{
			return (long)Math.Truncate(BigDecimalValue());
		}

		internal long LongValueExact()
		{
			decimal num = BigDecimalValue();
			decimal num2 = (long)Math.Truncate(num);
			if (num - num2 != 0m)
			{
				throw new ArithmeticException();
			}
			return (long)num2;
		}

		internal virtual double DoubleValue()
		{
			return decimal.ToDouble(BigDecimalValue());
		}

		public override string ToJsonString()
		{
			return BigDecimalValue().ToString();
		}

		internal abstract string GetJsonString();

		internal virtual float FloatValue()
		{
			return decimal.ToSingle(BigDecimalValue());
		}

		protected IExceptionFactory GetExceptionFactory()
		{
			return OracleJsonExceptions.ORACLE_FACTORY;
		}
	}
}
